############################################################################
# Code for producing figures in
# "Are There Social Class Gaps in Nascent Political Ambition? Survey Evidence from the Americas"
# Nicholas Carnes and Noam Lupu
# Code updated 8 April 2024
############################################################################

library(readstata13)
library(gplots)

setwd("C:/Users/lupun/Dropbox/Research/Nick and Noam/Ambition/Write Up/JOP submission/JOP Conditional Acceptance/Replication")
figure1 <- read.dta13("figure1.dta")
figurea2 <- read.dta13("figurea2.dta")


############################################################################
# FIGURE 1
############################################################################

upper = rev(100*(figure1$mean - figure1$ci))
lower = rev(100*(figure1$mean + figure1$ci))

par(mar=c(4, 10, 1, 1) + 0.1)
b <- barplot2(rev(figure1$mean)*100,
             names.arg=rev(figure1$country),
             xlim = range(0:100),
             las=1,
             xlab = "Proportion of Respondents",
             horiz = TRUE,
             plot.ci = TRUE, ci.u = upper, ci.l = lower, ci.width=0,
             plot.grid = TRUE)

box()


############################################################################
# FIGURE 2
############################################################################


working.class <- c(trait=0.2026278, encourage=0.3521619)
other.professions <- c(trait=0.2042704, encourage=0.333271)
occupation <- rbind(working.class*100, other.professions*100)

low.income <- c(trait=0.2646806, encourage=0.3377523)
high.income <- c(trait=0.2447732, encourage=0.3683625)
income <- rbind(low.income*100, high.income*100)

low.wealth <- c(trait=0.1686776, encouraged=0.3254652)
high.wealth <- c(trait=0.2079042, encouraged=0.3433083)
wealth <- rbind(low.wealth*100, high.wealth*100)

women <- c(trait=0.1813428, encouraged=0.2867491)
men <- c(trait=0.3181847, encouraged=0.3926597)
gender <- rbind(women*100, men*100)


par(mfrow=c(2,2))
par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(working.class*100, other.professions*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("working-class","other professions"),
             main="Occupation"
)
box()
text(b, occupation+5, labels=as.character(round(as.matrix(occupation), digits=0)))
text(2.1, 31, expression(italic('n.s.')))
text(5.3, 46, expression(italic('n.s.')))


par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(low.income*100, high.income*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("lower-income","higher-income"),
             main="Income"
)
box()
text(b, income+5, labels=as.character(round(as.matrix(income), digits=0)))
text(2.1, 35, expression(italic('n.s.')))
text(5.3, 48, expression(italic('n.s.')))


par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(low.wealth*100, high.wealth*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("lower-wealth","higher-wealth"),
             main="Wealth"
)
box()
text(b, wealth+5, labels=as.character(round(as.matrix(wealth), digits=0)))
text(2.1, 32, expression(italic('n.s.')))
text(5.3, 45, expression(italic('n.s.')))


par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(women*100, men*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("women","men"),
             main="Gender"
)
box()
text(b, gender+5, labels=as.character(round(as.matrix(gender), digits=0)))
text(2.1, 43, expression(italic('p<0.01')))
text(5.3, 50, expression(italic('p<0.02')))



############################################################################
# FIGURE A2
############################################################################

par(mar=c(4, 6, 2, 2) + 0.1)
plot(c(1,6),c(-15,15),type="n",axes=F,
     ylim=range(-15:15),
     xlim=range(1:6),
     ylab="Percentage-point gap in nascent ambition",
     xlab=""
)
box()
axis(1, at=seq(1,6,by=1), labels=c("1+ traits","2+ traits","3+ traits","4+ traits","5+ traits","6+ traits"), cex=0.8)
axis(2, at=seq(-15,15, by=5))
abline(h=0)
abline(h=c(-15,-10,-5,5,10,15),lty=3, col="grey")
lines(figurea2$group, figurea2$work*100, lty=1, col="black")
lines(figurea2$group, figurea2$lowinc*100, lty=2, col="gray50")
lines(figurea2$group, figurea2$lowwea*100, lty=4, col="gray25")
lines(figurea2$group, figurea2$fem*100, lty=5, col="gray75")
legend(x="topright", c("workers","lower-income","lower-wealth","women"), lty=c(1,2,4,5), col=c("black", "gray50", "gray25","gray75"))



############################################################################
# FIGURE A3
############################################################################

working.class <- c(trait=0.2406374, encourage=0.3379085)
other.professions <- c(trait=0.1663404, encourage=0.3295794)
occupation <- rbind(working.class*100, other.professions*100)

low.income <- c(trait=0.2715468, encourage=0.342403)
high.income <- c(trait=0.2519443, encourage=0.353248)
income <- rbind(low.income*100, high.income*100)

low.wealth <- c(trait=0.1913855, encouraged=0.3081628)
high.wealth <- c(trait=0.2011399, encouraged=0.3373448)
wealth <- rbind(low.wealth*100, high.wealth*100)

women <- c(trait=0.1890655, encouraged=0.2770514)
men <- c(trait=0.3201198, encouraged=0.3856447)
gender <- rbind(women*100, men*100)


par(mfrow=c(2,2))
par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(working.class*100, other.professions*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("working-class","other professions"),
             main="Occupation"
)
box()
text(b, occupation+5, labels=as.character(round(as.matrix(occupation), digits=0)))
text(2.1, 35, expression(italic('n.s.')))
text(5.3, 45, expression(italic('n.s.')))


par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(low.income*100, high.income*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("lower-income","higher-income"),
             main="Income"
)
box()
text(b, income+5, labels=as.character(round(as.matrix(income), digits=0)))
text(2.1, 38, expression(italic('n.s.')))
text(5.3, 46, expression(italic('n.s.')))


par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(low.wealth*100, high.wealth*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("lower-wealth","higher-wealth"),
             main="Wealth"
)
box()
text(b, wealth+5, labels=as.character(round(as.matrix(wealth), digits=0)))
text(2.1, 31, expression(italic('n.s.')))
text(5.3, 45, expression(italic('n.s.')))


par(mar=c(4, 4, 3, 1) + 0.1)
b <- barplot(rbind(women*100, men*100),
             beside = TRUE,
             space = c(0.2,1),
             ylim = range(0:100),
             ylab = "Proportion of respondents",
             names.arg = c("6+ politician\n traits","encouraged\n to run"),
             legend.text = c("women","men"),
             main="Gender"
)
box()
text(b, gender+5, labels=as.character(round(as.matrix(gender), digits=0)))
text(2.1, 43, expression(italic('p<0.01')))
text(5.3, 50, expression(italic('p<0.01')))


